package.cpath = package.cpath .. ";../../libs/?.so;../libs/?.so;../build/?.so"
package.path = package.path .. ";../../lua_libs/?.lua;../lua_libs/?.lua"

local packetbase = require "libpacketbase"
local logger = require "logger"


service_id = -1

function OnInit(id)
    logger.Info("gateway OnInit id: %s", id)
    service_id = id

    local ret = Lua2Cpp.Listen(8002, service_id)
    if ret < 0 then
        logger.Error("ua2Cpp.Listen error, %s", debug.traceback())
        return
    end

end

function OnSocketData(fd, len, buff)
    logger.Debug("gateway OnSocketData, fd:%s, len:%s, buff:%s", fd, len, buff)
    Lua2Cpp.EntireWrite(service_id, fd, buff)
end

function OnPacketData(fd, size, packet)
    logger.Debug("gateway OnPacketData, fd:%s, len:%s, packet:%s", fd, size, packet)
    local pack = packetbase:CreatePacket()
    pack:Copy(packet, size)

    local val = pack:ReadInt16()
    logger.Debug("test val:%s %s", val, debug.traceback())
    logger.Info("test val:%s", val)
    logger.Warn("test val:%s", val)
    logger.Error("test val:%s", val)
end

function OnExit()
    logger.Info("gateway OnExit, id:%s", service_id)
end